package entidades;

import gestordeentidades.persistance.DataBaseObject;

/**
 * Trabalho DAS - 2014/2015
 * @author a21210380, a21190325, a21210392
 */
public class Cor extends DataBaseObject {
    private double red;
    private double green;
    private double blue;
    private double opacity;

    public Cor(double red, double green, double blue, double opacity) {
        this.red = red;
        this.green = green;
        this.blue = blue;
        this.opacity = opacity;
    }

    public Cor() {
        this.red = 0;
        this.green = 0;
        this.blue = 0;
        this.opacity = 0;
    }

    public double getRed() {
        return red;
    }

    public Cor setRed(double red) {
        this.red = red;
        check();
        return this;
    }

    public double getGreen() {
        return green;
    }

    public Cor setGreen(double green) {
        this.green = green;
        check();
        return this;
    }

    public double getBlue() {
        return blue;
    }

    public Cor setBlue(double blue) {
        this.blue = blue;
        check();
        return this;
    }

    public double getOpacity() {
        return opacity;
    }

    public Cor setOpacity(double opacity) {
        this.opacity = opacity;
        check();
        return this;
    }
    
    public Cor set(double red, double green, double blue, double opacity){
        this.red = red;
        this.green = green;
        this.blue = blue;
        this.opacity = opacity;
        check();
        return this;
    }
    
    public void check() {
        if(red > 255.0) red = 255.0;
        if(green > 255.0) green = 255.0;
        if(blue > 255.0) blue = 255.0;
        if(opacity > 1.0) opacity = 1.0;
        
        if(red < 0.0) red = 0.0;
        if(green < 0.0) green = 0.0;
        if(blue < 0.0) blue = 0.0;
        if(opacity < 0.0) opacity = 0.0;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if(obj instanceof Cor) {
            Cor c = (Cor)obj;
            return red == c.getRed() && green == c.getGreen() && blue == c.getBlue() && opacity == c.getOpacity();
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 73 * hash + (int) (Double.doubleToLongBits(this.red) ^ (Double.doubleToLongBits(this.red) >>> 32));
        hash = 73 * hash + (int) (Double.doubleToLongBits(this.green) ^ (Double.doubleToLongBits(this.green) >>> 32));
        hash = 73 * hash + (int) (Double.doubleToLongBits(this.blue) ^ (Double.doubleToLongBits(this.blue) >>> 32));
        hash = 73 * hash + (int) (Double.doubleToLongBits(this.opacity) ^ (Double.doubleToLongBits(this.opacity) >>> 32));
        return hash;
    }
}
